﻿var settings = {
	addCount:0,
	initSwjgList:function(superSwjgId,level){
		var swjgListHtml = settings.swjgList(superSwjgId,level);
		$('#institutionTable').append(swjgListHtml);
	},
	swjgList:function(superSwjgId,level){
    	var html = '';
		$.ajax({
	        type: "GET",
	        url: config.baseURL + "settings/swjgList.action",
	        cache:false,
	        async: false,
	        dataType: "json",
	        data : {
	            "swjg.superSwjgId":superSwjgId,
	            "swjg.isDel":0
	        },
	        success: function(data){
	            if(data.result.success==true){
	            	$.each(data.swjgList, function(index, swjg) {
	            		var flag = '是'
	            		if(swjg.qybz != 0){
	            			flag = '否'
	            		}
	            		html += '<tr id="swjg'+swjg.id+'">'+
		                        '<td align="center" id="bm'+swjg.id+'">'+swjg.bm+'</td>'+
		                        '<td align="center" id="mc'+swjg.id+'">'+swjg.mc+'</td>'+
		                        '<td align="center" id="jc'+swjg.id+'">'+swjg.jc+'</td>'+
		                        '<td align="center" id="qc'+swjg.id+'">'+swjg.qc+'</td>'+
		                        '<td align="center" id="jb'+swjg.id+'">'+swjg.jb+'</td>'+
		                        '<td align="center" id="jj'+swjg.id+'">'+swjg.jj+'</td>'+
		                        '<td align="center" id="qybz'+swjg.id+'">'+flag+'</td>'+
		                        '<td align="center">';
		                if(level<2){
		                	html +='<a href="javascript:void(0);" id="openAddSwjgTable'+swjg.id+'" onclick="javascript:settings.appendAddSwjgTable('+swjg.id+','+(level+1)+');">展开</a>　|　';
	            		}
		                html += '<a href="javascript:void(0);" onclick="javascript:settings.showModifySwjgTable('+swjg.id+','+(level+1)+');">修改</a>　|　'+
		                        '<a href="javascript:void(0);" onclick="javascript:settings.removeSwjg('+swjg.id+')">删除</a>'+
		                        '</td>'+
		                    '</tr>';
	            	});
	            }else{
	                alert("获取失败");
	            }
	        },
	        error:function(msg){
	        	 alert("获取失败");
	        }
	    });
    	return html;
	},
	appendAddSwjgTable:function(swjgId,level){
		if(level<3){
			if($('#swjgChildTable'+swjgId).length==0){
				var btnValue = level+1==2?"增加县/区级机构":"增加科所级机构";
					
				var html = '<tr id="swjgChildTable'+swjgId+'">'+
				        '<td colspan="8" class="row_cont">'+
							'<table>'+
				                '<tr>'+
				                    '<th>编码</th>'+
				                    '<th>名称</th>'+
				                    '<th>简称</th>'+
				                    '<th>全称</th>'+
				                    '<th>级别</th>'+
				                    '<th>简介</th>'+
				                    '<th>是否有效</th>'+
				                    '<th>操作</th>'+
				                '</tr>';
				                html += settings.swjgList(swjgId,level);
				            html += '</table>'+
				            '<p><span class="btn"><input type="button" value="'+btnValue+'" class="J_addbtn" id="addXQBtn'+swjgId+'" onclick="javascript:settings.showAddSwjgTable('+swjgId+',\'addXQBtn'+swjgId+'\','+level+')"/></span></p>'+
				        '</td>'+
				    '</tr>';
				var swjgChildTable = $(html);
				$('#swjg'+swjgId).after(swjgChildTable);
			}
			
			var $td = $('#swjg'+swjgId).next().find("td").eq(0);
			if ($td.hasClass("row_cont")) {
				if ($td.is(":visible")) {
					$td.slideUp();
					$('#openAddSwjgTable'+swjgId).html("展开");
				} else {
					$td.slideDown();
					$('#openAddSwjgTable'+swjgId).html("收起");
				}
			}
		}
	},
	showAddSwjgTable:function(swjgId,btnId,level){
		$('#'+btnId).parents("p").prev().append('<tr><td><input type="text" id="bm'+settings.addCount+'" size="4"/></td><td><input type="text" id="mc'+settings.addCount+'"/></td>'+
			'<td><input type="text" id="jc'+settings.addCount+'"/></td><td><input type="text" id="qc'+settings.addCount+'"/></td><td><input type="text" id="jb'+settings.addCount+'"/></td>'+
			'<td><input type="text" id="jj'+settings.addCount+'"/></td><td><input type="text" size="1" id="qybz'+settings.addCount+'"/></td>'+
			'<td><button id="addSwjgBtn'+swjgId+'" onclick="javascript:settings.addSwjg('+swjgId+',\'bm'+settings.addCount+'\',\'mc'+settings.addCount+'\',\'jc'+settings.addCount+'\',\'qc'+settings.addCount+'\',\'jb'+settings.addCount+'\',\'sx'+settings.addCount+'\',\'qybz'+settings.addCount+'\',\'jj'+settings.addCount+'\',\'addSwjgBtn'+swjgId+'\','+level+');">保存</button>　'+
			'<button id="cancelAddSwjgBtn'+settings.addCount+'" onclick="javascript:settings.cancelAddSwjg('+settings.addCount+');">取消</button></td></tr>');
	},
	showModifySwjgTable:function(swjgId,level){
		var bm = $('#bm'+swjgId).text();
		var mc = $('#mc'+swjgId).text();
		var jc = $('#jc'+swjgId).text();
		var qc = $('#qc'+swjgId).text();
		var jb = $('#jb'+swjgId).text();
		var sx = $('#sx'+swjgId).text();
		var qybz = $('#qybz'+swjgId).text();
		var jj = $('#jj'+swjgId).text();
		$('#swjg'+swjgId).html('<td><input type="text" id="bm'+swjgId+'" size="4" value="'+bm+'"/></td><td><input type="text" id="mc'+swjgId+'" value="'+mc+'"/></td>'+
			'<td><input type="text" id="jc'+swjgId+'" value="'+jc+'"/></td><td><input type="text" id="qc'+swjgId+'" value="'+qc+'"/></td><td><input type="text" id="jb'+swjgId+'" value="'+jb+'"/></td>'+
			'<td><input type="text" id="jj'+swjgId+'" value="'+jj+'"/></td><td><input type="text" size="1" id="qybz'+swjgId+'" value="'+qybz+'"/></td>'+
			'<td><button id="modifySwjgBtn'+swjgId+'" onclick="javascript:settings.addSwjg('+swjgId+',\'bm'+swjgId+'\',\'mc'+swjgId+'\',\'jc'+swjgId+'\',\'qc'+swjgId+'\',\'jb'+swjgId+'\',\'sx'+swjgId+'\',\'qybz'+swjgId+'\',\'jj'+swjgId+'\',\'modifySwjgBtn'+swjgId+'\','+level+',1,'+swjgId+');">修改</button></td>');	
	},
	addSwjg:function(superSwjgId,bm,mc,jc,qc,jb,sx,qybz,jj,btnId,level,modifyFlag,swjgId){
		var bm = $('#'+bm).val();
		var mc = $('#'+mc).val();
		var jc = $('#'+jc).val();
		var qc = $('#'+qc).val();
		var jb = $('#'+jb).val();
		var sx = $('#'+sx).val();
		var qybz = $('#'+qybz).val();
		var jj = $('#'+jj).val();
		$.ajax({
	        type: "POST",
	        url: config.baseURL + "settings/addOrUpdateSwjg.action",
	        cache:false,
	        dataType: "json",
	        data : {
	            "swjg.superSwjgId":superSwjgId,
	            "swjg.bm":bm,
	            "swjg.mc":mc,
	            "swjg.jc":jc,
	            "swjg.qc":qc,
	            "swjg.jb":jb,
	            "swjg.sx":sx,
	            "swjg.qybz":qybz,
	            "swjg.jj":jj,
	            "swjg.id":swjgId,
	            "modifyFlag":modifyFlag
	        },
	        success: function(data){
	            if(data.result.success==true){
	            	var btnObj = $('#'+btnId).parent("td").parent("tr").parent();
	            	if(modifyFlag==1){
		            	alert("修改成功");
		            	$('#'+btnId).parent("td").parent("tr").remove(); 
	            	}else{
	            		alert("添加成功");
	            	}
	            	var sId = data.swjgId;
	            	btnObj.append('<tr id="swjg'+sId+'"><td id="bm'+sId+'">'+bm+'</td><td id="mc'+sId+'">'+mc+'</td><td id="jc'+sId+'">'+jc+'</td>'+
	        				'<td id="qc'+sId+'">'+qc+'</td><td>'+jb+'</td><td>'+jj+'</td><td>'+qybz+'</td>'+
	        				'<td align="center">'+
                    			(level!=2?'<a href="#" id="openAddSwjgTable'+data.swjgId+'" onclick="javascript:settings.appendAddSwjgTable('+data.swjgId+');">展开</a>　|　':'')+
		                        '<a href="#">修改</a>　|　'+
		                        '<a href="javascript:void(0);" onclick="javascript:settings.removeSwjg('+data.swjgId+')">删除</a>'+
	                    	'</td></tr>');	
	            	$('#cancelAddSwjgBtn'+settings.addCount).parent("td").parent("tr").remove();
	            }else{
	                alert("获取失败");
	            }
	        },
	        error:function(msg){
	        	alert("获取失败");
	        }
	    });
	},
	removeSwjg:function(id){
		if(confirm('确定删除本条税务机构设置?')){
			$.ajax({
		        type: "GET",
		        url: config.baseURL + "settings/deleteSwjg.action",
		        cache:false,
		        dataType: "json",
		        data : {
		            "swjg.id":id
		        },
		        success: function(data){
		            if(data.result.success==true){
		    			$('#swjg'+id).remove();	
		            }else{
		                alert("删除失败");
		            }
		        },
		        error:function(msg){
		        	alert("删除失败");
		        }
		    });
		}
	},
	cancelAddSwjg:function(btnId){
		$('#cancelAddSwjgBtn'+btnId).parent("td").parent("tr").remove(); 
	},
	accountList:function(){
		$.ajax({
	        type: "GET",
	        url: config.baseURL + "settings/swjgList.action",
	        cache:false,
	        async: false,
	        dataType: "json",
	        data : {
	            "swjg.isDel":0
	        },
	        success: function(data){
	            if(data.result.success==true){
	            	var html = "";
	            	$.each(data.swjgList, function(index, swjg) {
	            		html += '<option value="'+swjg.id+'">'+swjg.mc+'</option>';
	            	});
	            	$('#swjgId').html(html);
	            }else{
	                alert("获取失败");
	            }
	        }
	    });
		$.ajax({
            type: "GET",
            url: config.baseURL + "account/accountList.action",
            cache:false,
            dataType: "json",
	        data : {
	            "account.isDel":0
	        },
            success: function(data){
            	var html = "";
            	var sex ="" ;
            	var sfz ="";
            	var birthday="";
            	var xl = "";
            	var phone = "";
            	$.each(data.accountList, function(index, account) {
            		account[4]==null?sex=' ':sex = account[4];
            		account[5]==null?sfz=' ':sfz = account[5];
            		account[6]==null?birthday=' ':birthday = account[6];
            		account[7]==null?xl=' ':xl = account[7];
            		account[8]==null?phone=' ':phone = account[8];
		        	html += '<tr id="user'+account[0]+'">'+
		                '<td align="center">'+account[1]+'</td>'+
		                '<td align="center">'+account[2]+'</td>'+
		                '<td align="center">'+account[3]+'</td>'+
		                '<td align="center">'+sex+'</td>'+
		                '<td align="center">'+sfz+'</td>'+
		                '<td align="center">'+birthday+'</td>'+
		                '<td align="center">'+xl+'</td>'+
		                '<td align="center">'+phone+'</td>'+
		                '<td align="center">'+account[9]+'</td>'+
		                '<td align="center">'+account[10]+'</td>'+
		                '<td align="center">'+
		                    '<a href="javascript:settings.removeAccount('+account[0]+');">删除</a>'+
		                '</td>'+
		            '</tr>';
            	});
            	$("#accountListTable").html("");
            	$("#accountListTable").html(html);
            }
   		});
	},
	removeAccount:function(id){
		if(confirm('确定删除该用户？')){
			$.ajax({
		        type: "GET",
		        url: config.baseURL + "account/deleteAccount.action",
		        cache:false,
		        dataType: "json",
		        data : {
		            "account.id":id
		        },
		        success: function(data){
		            if(data.result.success==true){
		    			$('#user'+id).remove();	
		            }else{
		                alert("删除失败");
		            }
		        },
		        error:function(msg){
		        	alert("删除失败");
		        }
		    });
		}
	},
	addAccount:function(){
		var swjgId = $('#swjgId').val();
		var userName = $('#userName').val();
		var yhxm = $('#yhxm').val();
		var yhsfzh = $('#yhsfzh').val();
		var sex = $('#sex').val();
		var birthDate = $('#birthDate').val();
		var xl = $('#xl').val();
		var userphone = $('#userphone').val();
		var usertype = $('#usertype').val();
		var xybz = $('#xybz').val();
		if(userName){
			$.ajax({
		        type: "POST",
		        url: config.baseURL + "account/addAccount.action",
		        cache:false,
		        dataType: "json",
		        data : {
		            "account.swjgId":swjgId,
		            "account.userName":userName,
		            "account.yhxm":yhxm,
		            "account.yhsfzh":yhsfzh,
		            "account.sex":sex,
		            "account.birthDate":birthDate,
		            "account.xl":xl,
		            "account.userphone":userphone,
		            "account.usertype":usertype,
		            "account.xybz":xybz
		        },
		        success: function(data){
		            if(data.result.success==true){
		            	settings.accountList();
		            	$('#userName').val('');
		            	$('#yhxm').val('');
		            	$('#yhsfzh').val('');
		            	$('#birthDate').val('');
		            	$('#userphone').val('');
		            }else{
		                alert("获取失败");
		            }
		        },
		        error:function(msg){
		        	alert("获取失败");
		        }
		    });
		}else{
			alert("请检查参数");
		}
	},
	modifyAccountPassword:function(){
		var password = $('#password').val();
		var confirmPassword = $('#confirmPassword').val();
		if(!password){
			alert("密码不能为空");
		}else if(password!=confirmPassword){
			alert("两次密码不一致，请重新输入");
		}else{
			$.ajax({
		        type: "POST",
		        url: config.baseURL + "account/modifyAccountPassword.action",
		        cache:false,
		        dataType: "json",
		        data : {
		            "account.password":$('#password').val()
		        },
		        success: function(data){
		            if(data.result.success==true){
		            	$('#password').val("");
		            	$('#confirmPassword').val("");
		    			alert("修改成功");
		            }else{
		                alert("修改失败");
		            }
		        },
		        error:function(msg){
		        	 alert("修改失败");
		        }
		    });
		}
	}
};